package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.vo.AdminAddressVO;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.AdminAdressDtoMapper;
import com.starlink.model.domain.AdminAddressDO;
import com.starlink.mapper.AdminAddressMapper;
import com.starlink.model.request.AdminAdressRequest;
import com.starlink.service.AdminAddressService;
import com.starlink.model.util.AdminAddressConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class AdminAdressServiceImpl extends ServiceImpl<AdminAddressMapper, AdminAddressDO> implements AdminAddressService {
    @Autowired
    private AdminAddressMapper adminAddressMapper;

    @Override
    public AdminAddressVO getByKey(long id) {
        AdminAddressVO adminAdressVO = AdminAdressDtoMapper.INSTANCE.do2VO(getById(id));
        return adminAdressVO;
    }

    @Override
    public PageInfo<AdminAddressVO> pageQuery(Pageable pageable, AdminAdressRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<AdminAddressDO> list = adminAddressMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<AdminAddressVO> adminAdressVOList = AdminAddressConvert.buildAdminAdressVoList(list);
        return new PageInfo(adminAdressVOList);
    }

    @Override
    public List<AdminAddressVO> listQuery(AdminAdressRequest request) {
        List<AdminAddressDO> list = adminAddressMapper.selectDOList(AdminAdressDtoMapper.INSTANCE.request2DO(request));
        List<AdminAddressVO> adminAdressVOS = AdminAddressConvert.buildAdminAdressVoList(list);
        return adminAdressVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AdminAdressRequest request) {
        save(AdminAdressDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(AdminAdressRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(AdminAdressDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        adminAddressMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public AdminAddressVO getAddressInfo() {
        return AdminAdressDtoMapper.INSTANCE.do2VO(this.lambdaQuery().one());
    }
}
